#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void begin();
void get_command();
void division_command();
void exec_command();

char command[128];
char command_name[64];
char* command_parameter[64];
int main(){
	begin();
	return 0;
}
void begin(){
	while(1){
		get_command();
		if(strncmp(command,"exit",4)==0)break;
		division_command();
		exec_command();
	}
}
void get_command(){
	memset(command,0,sizeof(command));
	printf("=====>");
	fgets(command,sizeof(command),stdin);
	command[strlen(command)-1]='\0';
}
void division_command(){
	memset(command_name,0,sizeof(command_name));
	memset(command_parameter,0,sizeof(command_parameter));
	char* tok=strtok(command," ");
	strcpy(command_name,tok);
	int count=0;
	command_parameter[count++]=tok;
	while((tok=strtok(NULL," "))!=NULL){
		command_parameter[count++]=tok;
	}
}
void exec_command(){
	pid_t pid=0;
	pid=fork();
	if(pid==-1){
		perror("fork");
		exit(1);
	}
	if(pid==0){
		int res=execvp(command_name,command_parameter);
		if(res==-1){
			perror("execvp");
			exit(1);
		}
		exit(0);
	}else{
		while((waitpid(-1,NULL,WNOHANG))==0);	
	}
}
